package com.iisi.logical;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Utils {
    private static final String fixedNumber = "1987654321";

    /**
     * example: F212345674
     * **/
    public static String getCheckNum(final String originalString) {
        String firstSymbol = originalString.substring(0, 1);
        String otherSymbol = originalString.substring(1, originalString.length());
        String beforeCaculation = getSymbolCode().get(firstSymbol) + otherSymbol;
        String result = logicalCalulation(beforeCaculation, fixedNumber);
        return result;
    }

    public static void outputFile(final String fileName, List<String> data) {
        BufferedWriter bWriter = null;
        try {
            bWriter = new BufferedWriter(new FileWriter(new File(fileName)));
            
            for (String ss : data) {
            	if(ss.length()>0)
            		bWriter.write(ss);               
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bWriter != null) {
                try {
                    bWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static List<String> inputReadFile(final String fileName) {
        List<String> result = new ArrayList<String>();
        BufferedReader br = null;
        try {
            // br = new BufferedReader(new FileReader(new File(fileName)));
            br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName)), "Big5"));

            String line = br.readLine();
            do {
                result.add(line);
                line = br.readLine();
            } while (line != null);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    private static String logicalCalulation(final String parameter, final String fixedNumber) {
        String result = "";
        String imediateStr;
        try {
            StringBuffer sbf = new StringBuffer();
            char[] parameterAaary1 = parameter.toCharArray();
            char[] parameterAaary2 = fixedNumber.toCharArray();
            if (parameterAaary1.length != parameterAaary2.length) {
                throw new RuntimeException("統號位數必須相等於特定數位數");
            } else {
                for (int i = 0; i < parameterAaary1.length; i++) {
                    int initData = (Integer.valueOf(String.valueOf(parameterAaary1[i])).intValue())
                            * (Integer.valueOf(String.valueOf(parameterAaary2[i])).intValue());
                    String tmpString = String.valueOf(initData);
                    sbf.append(tmpString.charAt(tmpString.length() - 1));
                }
            }
            char[] parameterAaary3 = sbf.toString().toCharArray();
            int imediateNum = 0;
            for (char character : parameterAaary3) {
                imediateNum += Integer.valueOf(String.valueOf(character));
            }
            imediateStr = Integer.valueOf(imediateNum).toString();
            Integer numm = Integer.valueOf(String.valueOf(imediateStr.charAt(imediateStr.length() - 1)));
            if (numm.equals(0)) {
                result = "0";
            } else {
                result = String.valueOf(10 - numm);
            }

        } catch (NumberFormatException e) {
            e.printStackTrace();
            System.out.println("parameter: " + parameter + " ; fixedNumber: " + fixedNumber);
        }
        return result;
    }

    // private final static String retrieveLastChar(String )
    private static Map<String, String> getSymbolCode() {
        Map<String, String> result = new HashMap<String, String>();
        // 由於邏輯有不連號情形發生，故使用此法
        result.put("A", "10");
        result.put("B", "11");
        result.put("C", "12");
        result.put("D", "13");
        result.put("E", "14");
        result.put("F", "15");
        result.put("G", "16");
        result.put("H", "17");// 不連號發生
        result.put("J", "18");
        result.put("K", "19");
        result.put("L", "20");
        result.put("M", "21");
        result.put("N", "22");
        result.put("P", "23");
        result.put("Q", "24");
        result.put("R", "25");
        result.put("S", "26");
        result.put("T", "27");
        result.put("U", "28");
        result.put("V", "29");
        result.put("X", "30");
        result.put("Y", "31");
        result.put("W", "32");
        result.put("Z", "33");
        result.put("I", "34");
        result.put("O", "35");
        return result;
    }
}
